using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using DAL;
using System.Collections.Generic;

 
public partial class Customer_UCControls_UCGioHang : System.Web.UI.UserControl
{

    public static string srtPath;
    
    public static DangKy ItemDangKy = new DangKy();
    public static List<DangKy> ListDangKy = new List<DangKy>();
    public static List<GioHang> ListGioHang = new List<GioHang>();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((ListDangKy == null) || (ListDangKy.Count == 0))
            {
                ListDangKy = new List<DangKy>();
                ItemDangKy = (DangKy)Session["ItemDangKy"];
                ListDangKy.Add(ItemDangKy);
            }
            else
            {
                ListDangKy = (List<DangKy>)Session["ListDangKy"];
                ItemDangKy = (DangKy)Session["ItemDangKy"];
                ListDangKy.Add(ItemDangKy);
            }

            Session.Add("ListDangKy", ListDangKy);
            LoadGioHang(ListDangKy);
        }


    }

    protected void LoadGioHang(List<DangKy> List)
    {
        ListGioHang = new List<GioHang>();
        int id = 1;
        double ThanhTien = 0;
        foreach (DangKy ItemDangKy in List)
        {
            GioHang ItemGioHang = new GioHang();
            LoaiThe ItemLoaiThe = BAL.BALLoaiThe.GeById(ItemDangKy.IdLoaiThe);
            MauThe ItemMauThe = BAL.BALMauThe.GetById(ItemDangKy.IdMauThe);

            MenhGia ItemMenhGia = new MenhGia();
            MenhGiaKhac ItemMenhGiaKhac = new MenhGiaKhac();

            if (ItemDangKy.IdMenhGia != -1)
            {
                ItemMenhGia = BAL.BALMenhGia.GetById(ItemDangKy.IdMenhGia);
                ItemGioHang.MenhGia = ItemMenhGia.LoaiMenhGia.ToString();
            }
            else
            {
                ItemMenhGiaKhac = BAL.BALMenhGiaKhac.GetById(ItemDangKy.IdMenhGiaKhac);
                ItemGioHang.MenhGia = ItemMenhGiaKhac.LoaiMenhGia.ToString();
            }


            ItemGioHang.Id = id; id++;
            ItemGioHang.LoaiThe = ItemLoaiThe.TenThe.ToString();
            ItemGioHang.MauThe = ItemMauThe.DuongDan.ToString();
            ItemGioHang.SoLuong = ItemDangKy.SoLuong.ToString();
            ItemGioHang.ThanhTien = Convert.ToString(Convert.ToInt16(ItemGioHang.SoLuong) * Convert.ToInt32(ItemGioHang.MenhGia));
            ThanhTien = ThanhTien + Convert.ToInt32(ItemGioHang.ThanhTien);

            ListGioHang.Add(ItemGioHang);
        }

        lblTongCong.Text = ThanhTien.ToString("0,000.##");

        GridGioHang.DataSource = ListGioHang;
        GridGioHang.DataBind();


    }

    public string PathImage()
    {
        return srtPath = "images/Generic Card/MauThe/";
    }

    protected void LinkButtonMuaHangTiep_Click(object sender, EventArgs e)
    {
        ItemDangKy = new DangKy();
        Session.Remove("ItemDangKy");

        Session.Add("ListDangKy", ListDangKy);
        Response.Redirect("../Customer/ChuDe.aspx");
    }
    protected void LinkButtonThanhToan_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Customer/CamKetKhachHang.aspx");
    }
    protected void GridGioHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Xoa")
        {
            ListGioHang.RemoveAt(Convert.ToInt16(e.CommandArgument)-1);
            GridGioHang.DataSource = ListGioHang;
            GridGioHang.DataBind();

        }
    }
}
